<?php

namespace app\admin\controller;

/**
* 
*/
class Cate extends Base
{
	//列表
	public function list_l(){

		$cates=model('Cate')->order('sort','desc')->paginate(10);
		$viewData=[
			'cates'=>$cates
		];
		$this->assign($viewData);
		return view();
	}
	//添加
	public function add(){
		if(request()->isAjax()){
		$data=[
		'catename'=>input('post.catename'),
		'sort'=>input('post.sort')
		];
		$result=model('Cate')->add($data);


		if($result==1){
			$this->success('添加栏目成功！','admin/cate/list_l');//,'admin/cate/list'
		}else{
			$this->error($result);
		}
	}
		return view();
	}
	//排序
	public function sort(){
		$data=[
			'id'=>input('post.id'),
			'sort'=>input('post.sort')
		];
		 $result=model('Cate')->sort($data);
		if($result==1){
			$this->success('修改成功！');//,'admin/cate/list'
		}else{
			$this->error($result);
		}

	}
	//编辑
	public function edit(){
		$cateInfo=model('Cate')->find(input('id'));
		$viewData=[

		'cateInfo'=>$cateInfo
		];
		$this->assign($viewData);

		if(request()->isAjax()){
			$data=[
			'catename'=>input('post.catename'),
			'id'=>input('post.id')
			];
			$result=model('Cate')->edit($data);
			if($result==1){
			$this->success('修改成功！','admin/cate/list_l');//,'admin/cate/list'
		}else{
			$this->error($result);
		}
		}
		return view();
	}

	//删除栏目
	public function del(){
		$cateInfo=model('Cate')->with('article,article.content')->find(input('post.id'));
		foreach($cateInfo['article']as $k=>$v){
			$v->together('content')->delete();
		}
		$result=$cateInfo->together('article')->delete();
		
		if($result==1){
			$this->success('删除成功！');//,'admin/cate/list'
		}else{
			$this->error($result);
		}

	}

}






